#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 2e3 + 10;
const int MOD = 1e9 + 7;

int t, n, q, x, k;
int a[MAXN], b[MAXN][MAXN];

map<int, int> cnt;

bool check() {
    for (auto [k, v] : cnt) {
        if (k != v)
            return true;
    }
    return false;
}
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false), cin.tie(0);
    cin >> t;
    while (t--) {
        cnt.clear();
        cin >> n;
        for (int i = 1; i <= n;i++) {
            cin >> a[i];
            cnt[a[i]]++;
            b[0][i] = a[i];
        }
        int j = 1;

        while (check()) {
            for (int i = 1; i <= n; i++) {
                b[j][i] = cnt[b[j - 1][i]];
            }
            cnt.clear();
            for (int i = 1; i <= n; i++) {
                cnt[b[j][i]]++;
            }
            j++;
        }
        j--;
        cin >> q;
        while (q--) {
            cin >> x >> k;
            cout << b[min(j, k)][x] << endl;
        }
    }

    return 0;
}